/*   1:    */ package com.atomikos.util;
/*   2:    */ 
/*   3:    */ import java.io.Serializable;
/*   4:    */ import java.util.Hashtable;
/*   5:    */ import javax.naming.Context;
/*   6:    */ import javax.naming.Name;
/*   7:    */ import javax.naming.NameNotFoundException;
/*   8:    */ import javax.naming.NamingException;
/*   9:    */ import javax.naming.Reference;
/*  10:    */ import javax.naming.StringRefAddr;
/*  11:    */ import javax.naming.spi.ObjectFactory;
/*  12:    */ 
/*  13:    */ public class IntraVmObjectFactory
/*  14:    */   implements ObjectFactory
/*  15:    */ {
/*  16:    */   private static final String NAME_REF_ADDRESS_TYPE = "uniqueResourceName";
/*  17: 54 */   private static SerializableObjectFactory serializableObjectFactory = new SerializableObjectFactory();
/*  18:    */   
/*  19:    */   private static synchronized Object retrieveObjectInstance(Object obj, Name name, Context nameCtx, Hashtable environment)
/*  20:    */     throws Exception
/*  21:    */   {
/*  22: 61 */     Object ret = null;
/*  23: 64 */     if ((obj instanceof Reference))
/*  24:    */     {
/*  25: 65 */       String resourceName = null;
/*  26: 66 */       Reference ref = (Reference)obj;
/*  27: 67 */       StringRefAddr nameAsRefAddr = (StringRefAddr)ref.get("uniqueResourceName");
/*  28: 68 */       resourceName = (String)nameAsRefAddr.getContent();
/*  29:    */       try
/*  30:    */       {
/*  31: 70 */         ret = IntraVmObjectRegistry.getResource(resourceName);
/*  32:    */       }
/*  33:    */       catch (NameNotFoundException notYetInited)
/*  34:    */       {
/*  35: 73 */         Object resource = serializableObjectFactory.getObjectInstance(obj, name, nameCtx, environment);
/*  36: 74 */         IntraVmObjectRegistry.addResource(resourceName, resource);
/*  37: 75 */         ret = resource;
/*  38:    */       }
/*  39:    */     }
/*  40: 79 */     return ret;
/*  41:    */   }
/*  42:    */   
/*  43:    */   public static synchronized Reference createReference(Serializable object, String name)
/*  44:    */     throws NamingException
/*  45:    */   {
/*  46: 84 */     Reference ret = null;
/*  47: 85 */     if (object == null) {
/*  48: 85 */       throw new IllegalArgumentException("invalid resource: null");
/*  49:    */     }
/*  50: 86 */     if (name == null) {
/*  51: 86 */       throw new IllegalArgumentException("name should not be null");
/*  52:    */     }
/*  53:    */     try
/*  54:    */     {
/*  55: 90 */       Object existing = IntraVmObjectRegistry.getResource(name);
/*  56: 91 */       if (existing != object)
/*  57:    */       {
/*  58: 93 */         String msg = "Another resource already exists with name " + name + " - pick a different name";
/*  59: 94 */         throw new NamingException(msg);
/*  60:    */       }
/*  61:    */     }
/*  62:    */     catch (NameNotFoundException notThere)
/*  63:    */     {
/*  64:100 */       IntraVmObjectRegistry.addResource(name, object);
/*  65:    */     }
/*  66:102 */     ret = SerializableObjectFactory.createReference(object, IntraVmObjectFactory.class.getName());
/*  67:    */     
/*  68:104 */     ret.add(new StringRefAddr("uniqueResourceName", name));
/*  69:105 */     return ret;
/*  70:    */   }
/*  71:    */   
/*  72:    */   public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable environment)
/*  73:    */     throws Exception
/*  74:    */   {
/*  75:111 */     return retrieveObjectInstance(obj, name, nameCtx, environment);
/*  76:    */   }
/*  77:    */ }


/* Location:           C:\myfile\mysoft\apache-maven-3.5.0-bin\apache-maven-3.5.0\myrep\com\atomikos\atomikos-util\3.7.0\atomikos-util-3.7.0.jar
 * Qualified Name:     com.atomikos.util.IntraVmObjectFactory
 * JD-Core Version:    0.7.0.1
 */